<?php
	if (!defined('EXPONENT')) exit('');
	if (exponent_permissions_check(array('administrate','configure',"create"),$loc)){
		//// pagination
		$sql_where = "(location_data = '".serialize($loc)."')";
		$obj = null;
		
		if(isset($_REQUEST['cat_name']))
		{   
			$cat_name = _ab_safe_html_string($_REQUEST['cat_name']);
			$sql_where .= " AND (name LIKE '%{$cat_name}%')";
		}
		
		// sort
		$sort="";
		if (isset($_REQUEST['sortName']))
		{
			$sort=$_REQUEST['sortName'];
				
			$sortOrder="ASC";
			if (isset($_REQUEST['sortOrder']) && ($_REQUEST['sortOrder'] != "asc"))
				$sortOrder = "DESC";
			$sort = " ORDER BY {$sort} {$sortOrder}";
		}
		else
			$sort = "ORDER BY id DESC";
		
		$page=1;
		if (isset($_REQUEST['page']))
			$page=intval($_REQUEST['page']);
		$pageSize=10;
		
		if (isset($_REQUEST['pageSize']))
			$pageSize=intval($_REQUEST['pageSize']);
		$start=($page-1)*$pageSize;

		$data=$db->selectObjects('surveymodule_companies',"{$sql_where} {$sort} limit {$start},{$pageSize}");
		
		$ret_object = null;
		$ret_object->total = $db->countObjects('surveymodule_companies', $sql_where);
		$ret_object->rows = $data;
		
		echo json_encode($ret_object);
	}
	die();
?>